HTMLElement: focus() method

您所在的位置:网站首页 force button HTMLElement: focus() method

HTMLElement: focus() method

#HTMLElement: focus() method| 来源: 网络整理| 查看: 265

This example demonstrates how you can set the focus on a button element.

HTML

First we define three buttons. Both the middle and right button will set focus on the left-most button. The right right-most button will also specify focusVisible.

htmlButton Click to set focus on "Button" Click to set focus and focusVisible on "Button" JavaScript

The code below sets up handlers for click events on the middle and right buttons.

jsdocument.getElementById("focusButton").addEventListener("click", () => { document.getElementById("myButton").focus(); }); document .getElementById("focusButtonVisibleIndication") .addEventListener("click", () => { document.getElementById("myButton").focus({ focusVisible: true }); }); Result

Select either the middle button or right-most button to set focus on the left-most button.

Browsers do not usually show visible focus indication on button elements when focus is set programmatically, so the effect of selecting the middle button may not be obvious. However provided the focusVisible option is supported on your browser, you should see focus changing on the left-most button when the right-most button is selected.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3